//
//  StockDetailVC.m
//  MyStock
//
//  Created by 刘翔 on 2024/10/22.
//

#import "StockDetailVC.h"
#import "StockWebDFVC.h"
#import "StockWebKplVC.h"

@interface StockDetailVC()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>


@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *listVCArray;
@property (nonatomic, strong) JXCategoryListContainerView *listVCContainerView;
@property (nonatomic, strong) NSArray *titles;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UILabel *price1L;
@property (weak, nonatomic) IBOutlet UILabel *price2L;

@property (weak, nonatomic) IBOutlet UILabel *nameL;
@property (weak, nonatomic) IBOutlet UILabel *codeL;
@property (weak, nonatomic) IBOutlet StockLabel *diffL;

@property (weak, nonatomic) IBOutlet UILabel *lowPriceL;


@end

@implementation StockDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.customNavBar.title = self.name;
    
    WeatherLabel *label = [[WeatherLabel alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    label.attributedText = [[NSAttributedString alloc] initWithString:[AppService shareInstance].weather];
    [self.customNavBar addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.customNavBar).offset(-10);
        make.right.equalTo(self.customNavBar).offset(-20);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(70);
    }];
    self.lowPriceL.textColor = Color_gree;
    self.lowPriceL.text = [NSString stringWithFormat:@"%.2f",self.lowPrice];
    
    self.titles = @[@"开盘啦",@"东方财富"];
    self.categoryView = [[JXCategoryTitleView alloc] init];
    self.categoryView.frame = CGRectMake(0, Height_NavBar + 70, kScreenWidth,44);
    self.categoryView.delegate = self;
    self.categoryView.titles = self.titles;
    self.categoryView.titleColor = [UIColor colorWithHexString:@"#333333"];
    self.categoryView.titleSelectedColor = [UIColor colorWithHexString:@"#FB78A3"];
    self.categoryView.titleFont = [UIFont systemFontOfSize:14];
    self.categoryView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    self.categoryView.defaultSelectedIndex = 0;
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.lineStyle = JXCategoryIndicatorLineStyle_Normal;
    lineView.lineScrollOffsetX = 5;
    lineView.indicatorColor = [UIColor colorWithHexString:@"#FB78A3"];
    self.categoryView.indicators = @[lineView];
    [self.view addSubview:self.categoryView];
    
    self.listVCContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    self.listVCContainerView.frame = CGRectMake(0, Height_NavBar + 44 + 70, kScreenWidth, kScreenHeight - Height_NavBar - kBottomSafeHeight - 44 - 70);
    self.listVCContainerView.defaultSelectedIndex = 0;
    [self.view addSubview:self.listVCContainerView];
    
    self.categoryView.contentScrollView = self.listVCContainerView.scrollView;
    
    self.price1L.text = self.cost15;
    self.price2L.text = self.cost30;
    self.nameL.text = self.name;
    self.codeL.text = self.code;
    self.diffL.code = self.code;
    __weak typeof(self) weakSelf = self;
    self.diffL.stockChangeHandle = ^(StockModel * _Nullable stockData) {
        
        double diff = ([stockData.last_px doubleValue] - [weakSelf.cost30 doubleValue]) / [weakSelf.cost30 doubleValue] * 100;
        weakSelf.diffL.text = [NSString stringWithFormat:@"%.2f",diff];
        if (diff > 0) {
            weakSelf.diffL.textColor = Color_gree;
        }else if (diff == 0){
            weakSelf.diffL.textColor = Color_3;
        }else{
            weakSelf.diffL.textColor = Color_red;
        }
        
        
    };
    
    
    
}

#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index
{
    
    [self.listVCContainerView didClickSelectedItemAtIndex:index];
    
}

#pragma mark - JXCategoryListContainerViewDelegate
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    if (index == 0) {
        StockWebKplVC *vc = [[StockWebKplVC alloc] init];
        NSMutableString *urlStr = [NSMutableString stringWithFormat:@"%@code=%@",WEBURL,self.code];
        if([self.cost15 floatValue] > 0){
            [urlStr appendFormat:@"&line15=%@",self.cost15];
        }
        if([self.cost30 floatValue] > 0){
            [urlStr appendFormat:@"&line30=%@",self.cost30];
        }
        vc.url = [NSURL URLWithString:urlStr];
        return vc;
        
    }else{
        
        StockWebDFVC *vc = [[StockWebDFVC alloc] init];
        NSString *urlStr;
        if([[self.code substringToIndex:1] isEqualToString:@"6"]){
            urlStr = [NSString stringWithFormat:@"https://wap.eastmoney.com/quote/stock/1.%@.html",self.code];
        }else{
            urlStr = [NSString stringWithFormat:@"https://wap.eastmoney.com/quote/stock/0.%@.html",self.code];
        }
        vc.url = [NSURL URLWithString:urlStr];
        return vc;
    }
    
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}


- (UIColor *)getTextColorWithModel:(StockModel *)model{
    if ([model.px_change_rate doubleValue] > 0) {
        return Color_red;
    }else if ([model.px_change_rate doubleValue] == 0){
        return Color_3;
    }else{
        return Color_gree;
    }
}


@end
